package cn.kgc.configuration;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * 这个配置类配置数据库相关的信息
 */
@Configuration
@ComponentScan(basePackages="cn.kgc")
public class DBConfiguration {
    /**
     * 创建DataSource对象并且添加到IOC容器中
     */
    @Bean
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/smbms");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        return dataSource;
    }
    /**
     * 创建QueryRunner对象并且添加到IOC容器中
     */
    @Bean
    public QueryRunner getQueryRunner(DataSource dataSource){
        QueryRunner queryRunner=new QueryRunner(dataSource);
        return queryRunner;
    }

}
